define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'item/item/index',
                    add_url: 'item/item/add',
                    edit_url: 'item/item/edit',
                    del_url: 'item/item/del',
                    multi_url: 'item/item/multi',
                    table: 'item',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'itemname', title: __('Itemname')},
                        {field: 'sku', title: __('Sku')},
                        {field: 'beforelong', title: __('Volume'),operate:false,formatter:Controller.api.formatter.volume},
                        {field: 'beforeweight', title: __('Weight'),operate:false},
                        {field: 'source', title: __('Source'),operate:false},
                        {field: 'describe', title: '关联SKU',operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible:true
            });

            $(document).on('click','.print-item',function (){
                var that = this;
                var ids = Table.api.selectedids(table);

                var url = "item/item/printItem?ids="+ids;
                //ids = ($.isArray(ids) ? ids.join(",") : ids);
                //url = Table.api.replaceurl(url,{ids:ids},table); window.alert(url);
                url = Fast.api.fixurl(url);
                //for(var i=3;i>0;i--){
                //$.get(url);
                window.location = url;
                //}
                return;
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on('click','.add-sku',function () {
                var html = "<div class=\"list-content\">\n" +
                    "                        <div class=\"pd-item\">\n" +
                    "                            <input type=\"text\" name=\"row[rsku][]\" value=\"\">\n" +
                    "                            <input type=\"text\" name=\"row[name][]\" value=\"\">\n" +
                    "                            <select class=\"form-control\" name=\"row[itemsource][]\">\n" +
                    "                                <option value=\"Aliexpress\">Aliexpress</option>\n" +
                    "                                <option value=\"Amazon,\">Amazon</option>\n" +
                    "                                <option value=\"Ebay\">Ebay</option>\n" +
                    "                                <option value=\"Wish\">Wish</option>\n" +
                    "                            </select>\n" +
                    "                        </div>\n" +
                    "                    </div>";
                $('.products-list').append(html);
            });

            $(document).on('click','.remove-item',function () {
                    $(this).parent().empty();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
               volume: function(value,row,index){
                   return row.beforelong+'*'+row.beforewidth+'*'+row.beforehigh;
               }
            }
        }
    };
    return Controller;
});